import 'package:enterprise_v6_app/app/components/special_operation/custom_high_input_page.dart';
import 'package:enterprise_v6_app/app/components/special_operation/custom_multi_signature_page.dart';
import 'package:enterprise_v6_app/app/components/special_operation/custom_picker_photo.dart';
import 'package:enterprise_v6_app/app/components/special_operation/custom_select_button_page.dart';
import 'package:enterprise_v6_app/app/components/special_operation/measure_dynamic_editable.dart';
import 'package:enterprise_v6_app/app/components/special_operation/measure_dynamic_select.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/widget_extension.dart';
import 'package:enterprise_v6_app/app/data/common/simple_choice_bean.dart';
import 'package:enterprise_v6_app/app/data/entities/application/execute_entity.dart';

///特殊作业-自定义安全措施组件
///[safetyMeasures] 安全措施数据
///[isEnabled] 是否可编辑
///[isChange] 是否在交接班中
///[index]序号
class CustomSafetyMeasurePage extends StatefulWidget {
  const CustomSafetyMeasurePage({required this.safetyMeasures, required this.isChange, required this.index, super.key, this.isEnabled = true});

  final ExecuteWorkSafetyMeasuresList safetyMeasures;
  final bool isEnabled;
  final bool isChange;
  final int index;

  @override
  State<CustomSafetyMeasurePage> createState() => _CustomSafetyMeasurePageState();
}

class _CustomSafetyMeasurePageState extends State<CustomSafetyMeasurePage> {
  /// 是否涉及
  List<SimpleChoiceBean> choiceDataList = [];

  /// 可输入时显示的安全措施
  String replacedString = '';

  /// 原本的安全措施
  String startingSafetyMeasures = '';

  @override
  void initState() {
    super.initState();
    choiceDataList.add(SimpleChoiceBean(id: '1', name: '是', isChecked: widget.safetyMeasures.isInvolve == 1));
    choiceDataList.add(SimpleChoiceBean(id: '0', name: '否', isChecked: widget.safetyMeasures.isInvolve == 0));
    startingSafetyMeasures = widget.safetyMeasures.safetyMeasures!;
  }

  ///是否涉及
  Widget _getIsInvolvedWidget() {
    return Row(
      children: [
        Text(
          '是否涉及：',
          style: TextStyle(fontSize: 14.sp, height: 1.2, color: const Color(0xFF262626)),
        ),
        AppGaps.hGap50,
        Expanded(
            child: CustomSelectButtonPage<SimpleChoiceBean>(
                dataList: choiceDataList,
                isEnabled: widget.isEnabled && !widget.isChange,
                onConfirm: (List<SimpleChoiceBean> value) {
                  if (widget.safetyMeasures.isInvolve == int.parse(value[0].getId())) {
                    return;
                  }
                  // 选择否时，清空输入和选择
                  widget.safetyMeasures.isInvolve = int.parse(value[0].getId());
                  widget.safetyMeasures.userSign!.clear();
                  widget.safetyMeasures.images!.clear();
                  widget.safetyMeasures.safetyMeasures = startingSafetyMeasures;
                  // if (widget.safetyMeasures.isInvolve == 0 &&
                  //     widget.safetyMeasures.userSign != null) {
                  //   widget.safetyMeasures.userSign!.clear();
                  // }
                  setState(() {});
                }))
      ],
    );
  }

  ///获取图片上传显示
  Widget _getImageUpdate() {
    return Column(
      children: [
        Divider(
          height: 1.w,
          color: AppColors.bg_color_grey,
        ),
        CustomPickerPhoto(
          title: '图片上传',
          isEnabled: widget.isEnabled,
          imagesList: widget.safetyMeasures.images!,
          titleTextStyle: TextStyle(
            color: const Color(0xFF262F38),
            fontSize: 14.sp,
            fontWeight: FontWeight.w400,
          ),
          callback: (List<String> path) {
            widget.safetyMeasures.images = path;
          },
        )
      ],
    );
  }

  ///判断安全措施是否可输入或选择
  void _getReplacedString() {
    // 包含'-^'为可以输入编辑的类型
    if (widget.safetyMeasures.safetyMeasures!.contains('-^')) {
      replacedString = widget.safetyMeasures.safetyMeasures!.replaceAll('-^', '').replaceAll('+^', '');
    }
    // 包含'-<'为选择类型
    if (widget.safetyMeasures.safetyMeasures!.contains('-<')) {
      replacedString = widget.safetyMeasures.safetyMeasures!.replaceAll('-<', '').replaceAll('>-', '').replaceAll('-~', '').replaceAll('+~', '✔');
    }
  }

  @override
  Widget build(BuildContext context) {
    _getReplacedString();
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 16.w),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(8.w),
      ),
      child: Column(
        children: [
          CustomHighInputPage(
            // title: '安全措施${widget.index == null ? '' : widget.index! + 1}:',
            title: '${widget.index + 1} -- ${widget.safetyMeasures.controlPointName}:',
            text: replacedString == '' ? (widget.safetyMeasures.safetyMeasures ?? '') : replacedString,
            onCallBack: (value) {},
            isRequired: false,
            isEnabled: false,
          ).clickOnce(() {
            // 没有可编辑内容、不可编辑、选中不涉及、在交接班中,点击无效
            if (replacedString.isEmpty || !widget.isEnabled || widget.safetyMeasures.isInvolve == 0 || widget.isChange) {
              return;
            }
            //可输入的编辑弹框
            if (widget.safetyMeasures.safetyMeasures!.contains('-^')) {
              // 显示可输入的编辑弹框
              Get.dialog<String>(MeasureDynamicEditable(
                text: widget.safetyMeasures.safetyMeasures!,
                onCallback: (value) {
                  // 确定回调赋值
                  widget.safetyMeasures.safetyMeasures = value;
                  setState(() {});
                },
              ));
            }
            //选择类型
            if (widget.safetyMeasures.safetyMeasures!.contains('-<')) {
              // 显示选择类型的编辑弹框
              Get.dialog<String>(MeasureDynamicSelect(
                text: widget.safetyMeasures.safetyMeasures!,
                onCallback: (value) {
                  // 确定回调赋值
                  widget.safetyMeasures.safetyMeasures = value;
                  setState(() {});
                },
              ));
            }
          }),
          // 是否涉及
          _getIsInvolvedWidget(),
          Divider(
            height: 1.w,
            color: AppColors.bg_color_grey,
          ),
          // 如果是其他安全措施,需要增加编制人签字
          Visibility(
            visible: widget.safetyMeasures.safetyMeasures!.contains('其他安全措施'),
            child: Column(
              children: [
                CustomMultiSignaturePage(
                  title: '编制人',
                  isEnabled: widget.isEnabled,
                  isChange: widget.isChange,
                  signatureLists: widget.safetyMeasures.preparedSign ?? [],
                  onCallBack: (value) {
                    widget.safetyMeasures.preparedSign = value;
                  },
                ),
                Divider(
                  height: 1.w,
                  color: AppColors.bg_color_grey,
                ),
              ],
            ),
          ),
          CustomMultiSignaturePage(
            title: widget.safetyMeasures.controlAuthName!,
            isEnabled: widget.isEnabled,
            isChange: widget.isChange,
            signatureLists: widget.safetyMeasures.userSign ?? [],
            onCallBack: (value) {
              widget.safetyMeasures.userSign = value;
            },
          ),
          Visibility(
            visible: widget.safetyMeasures.isInvolve == 1,
            child: _getImageUpdate(),
          )
        ],
      ),
    );
  }
}
